package oceanlife.log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

public class LogParser {
	
	public static String kind = "test";

	public static void main(String[] args){
		try {
			BufferedReader in = new BufferedReader(new FileReader("C:/Users/Marijn/workspaceJAVAnew/OCEAN LIFE/linux/logs/log"+kind+".txt"));
			BufferedWriter out = new BufferedWriter(new FileWriter("C:/Users/Marijn/workspaceJAVAnew/OCEAN LIFE/linux/logs/out"));
			BufferedWriter render = new BufferedWriter(new FileWriter("C:/Users/Marijn/workspaceJAVAnew/OCEAN LIFE/linux/logs/render"));
			render.write("set terminal png\n");
			String line = in.readLine();
			boolean firstColom = true;
			int lastGen = Integer.MAX_VALUE;
			ArrayList<Child> children = new ArrayList<Child>();
			ArrayList<Double> values = new ArrayList<Double>();
			int interval = 0;
			int scenarioCount = 0;
			int agents = 0;
			HashMap<Integer,Integer> intervals = new HashMap<Integer, Integer>();
			while(line != null){
				String[] pieces = line.split(" ");
				if(pieces[0].equals("Agent_spawn_interval:")){
					interval = Integer.parseInt(pieces[1]);
					int count = 1;
					if(intervals.get(interval) != null){
						count += intervals.get(interval);
					}
					intervals.put(interval, count);
				}
				if(pieces[0].equals("Scenario_count:")){
					scenarioCount = Integer.parseInt(pieces[1]);
				}
				if(pieces[0].equals("Agents_per_Scenario:")){
					agents = Integer.parseInt(pieces[1]);
				}
				if(pieces[0].equals("Generation:")){
					int gen = Integer.parseInt(pieces[1]);
					if(gen < lastGen){
						out.close();
						String name = kind+interval+"-"+intervals.get(interval);
						out = new BufferedWriter(new FileWriter("C:/Users/Marijn/workspaceJAVAnew/OCEAN LIFE/linux/logs/"+name));
						render.write("set output '"+name+".png'\n");
						render.write("set yrange [1e+06:1.5e+06]\n");
						render.write("set xrange [0:180]\n");
						render.write("plot '"+name+"' using 1:3:2:6:5 with candlesticks\n");
					}
					lastGen = gen;
				}
				int teller = 0;
				//System.out.println(line);
				if(pieces[0].equals("Child:")){
					Child child = new Child();
					child.fitness = Double.parseDouble(pieces[2])/(agents*scenarioCount);
					child.stepsize = Double.parseDouble(pieces[4]);
					children.add(child);
				}
				if(pieces[0].equals("Best_Child:")){
					Collections.sort(children);
					values.add((double) lastGen);
					double upper,upperMid,median,lowerMid,lower;
					upper = children.get(0).fitness;
					upperMid = children.get((int)Math.round(children.size()/4.0)-1).fitness;
					if(children.size() % 2 == 0){
						median = (double)(children.get((children.size()/2)-1).fitness+children.get((children.size()/2)).fitness)/2.0;
					} else {
						median = (double)(children.get((children.size()/2)).fitness);
					}
					lowerMid = children.get((int)Math.round(children.size()*3.0/4.0)-1).fitness;
					lower = children.get(children.size()-1).fitness;
					
					
					values.add(upper);
					values.add(upperMid);
					values.add(median);
					values.add(lowerMid);
					values.add(lower);
					for(Double value : values){
						if(!firstColom){
							out.write(' ');
						}
						firstColom = false;
						out.write(value.toString());
					}
					out.write('\n');
					children.clear();
					values.clear();
				}
				line = in.readLine();
			}
			in.close();
			out.close();
			render.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
